package com.test.vue.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.test.vue.entity.CheckUserDate;
import com.test.vue.entity.LoginUser;
import com.test.vue.entity.User;
import com.test.vue.service.UserService;
import com.test.vue.utils.CookieUtils;
import com.test.vue.utils.ReturnResult;
import com.test.vue.utils.RegexValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //用户登录
    @JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss")
    @RequestMapping(value = "/login", method = { RequestMethod.POST, RequestMethod.GET },consumes = "application/json; charset=UTF-8")
    public ReturnResult login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) {
        //@RequestBody注解自动将前端的参数封装到了loginUser对象中
        String username = user.getUsername();
        String password = user.getPassword();
        ReturnResult result = userService.login(username, password, request, response);
        System.out.println("登陆成功");
        return result;
    }


    //校验用户名,前端注册页面输入账号有一个失焦事件,向后端发送异步请求,后端来判断用户名是否被占用,将结果返回前端
    @RequestMapping(value="/checkUsername",method = RequestMethod.POST)
    public CheckUserDate checkUsername(@RequestBody User user){
        String username = user.getUsername();
        System.out.println(username);
        CheckUserDate result = userService.checkUsername(username);
        return result;
    }

    /**
     *
     * @param user
     * @return
     */
    //注册,用户提交数据进行注册操作
    @RequestMapping(value="/regist",method = { RequestMethod.POST, RequestMethod.GET })
    public ReturnResult regist(@RequestBody User user){
        ReturnResult result = userService.regist(user);
        return result;
    }
    /**
     校验手机号,前端校验手机号格式以及用户名格式交给我了,用@RequestParam报400错误,前后端的传接收的数据不一致决定的
     前端 非要传json类型的过来,我也没办法...
     */
    @RequestMapping(value = "/checkPhoneNum",method = { RequestMethod.POST, RequestMethod.GET })
    public CheckUserDate checkPhoneNum(@RequestBody User user){
        String telephone = user.getTelephone();
        boolean b = RegexValidateUtil.checkCellphone(telephone);
        if(b==false){
            return CheckUserDate.build(400,"手机号输入不合法");
        }
        return CheckUserDate.build(200,"手机号格式正确");
    }

    /**
     * 用户退出
     * 这里很想吐槽,前端如果还是坚持要给传json类型过来的话,我也只能单独给这个字段建一个类了,用@RequestBody去填充
     * @return
     */
    @RequestMapping(value = "/userExit",method =RequestMethod.POST )
    public CheckUserDate userExit(@RequestParam(value = "token",required = false) String token,HttpServletResponse response){
        //用户退出删除cookie里的数据和redis里数据
        CookieUtils.removeCookie(response,"LOGIN_COOKIE");
        Jedis jedis = new Jedis();
        jedis.del("token");
        System.out.println(token);
        return CheckUserDate.build(200,"用户退出成功");
    }
    /**
     * 用户的单点登录,防止重复登录
     */
    @RequestMapping(value = "/checkToken",method =RequestMethod.GET)
    public ReturnResult checkToken(@RequestParam(value = "token",required = false) String token,HttpServletRequest request, HttpServletResponse response){
        //获取到传来的token值,用token查询本地redis中的数据
        Jedis jedis = new Jedis();
        String userInf = jedis.get(token);
        if(userInf==null||!token.equals(CookieUtils.getUid(request,"LOGIN_COOKIE"))){
            //如果查询不到用户数据,或者传来的token值与查询到的token值不相同
            return ReturnResult.build(400,"需要重新登录");
        }
        //重新设定redis中的时间
        jedis.expire(token,1800);
        //重新设定cookie中的时间
        CookieUtils.addCookie(response,"LOGIN_COOKIE",token,1800);
        return ReturnResult.build(200,"登录成功",token,userInf);

    }
}
